package com.panasonic.web.service.impl;


import com.panasonic.web.domain.cms.CmsArticle;
import com.panasonic.web.domain.cms.CmsAttachment;
import com.panasonic.web.mapper.CmsArticleMapper;
import com.panasonic.web.service.ICmsArticleService;
import com.panasonic.web.util.CmsSecurityUtils;
import com.panasonic.web.vo.CmsArticleMoveCategoryVo;
import com.panasonic.web.vo.CmsArticleTopChangeVo;
import com.panasonic.common.utils.DateUtils;
import com.panasonic.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 文章Service业务层处理
 *
 * @author lwf
 * @date 2021-11-12
 */
@Service
public class CmsArticleServiceImpl implements ICmsArticleService {
    @Autowired
    private CmsArticleMapper cmsArticleMapper;

    /**
     * 查询文章
     *
     * @param id 文章ID
     * @return 文章
     */
    @Override
    public CmsArticle selectCmsArticleById(Long id) {
        return cmsArticleMapper.selectCmsArticleById(id);
    }

    /**
     * 查询文章列表
     *
     * @param cmsArticle 文章
     * @return 文章
     */
    @Override
    public List<CmsArticle> selectCmsArticleList(CmsArticle cmsArticle) {
        return cmsArticleMapper.selectCmsArticleList(cmsArticle);
    }

    /**
     * 修改文章
     *
     * @param cmsArticle 文章
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCmsArticle(CmsArticle cmsArticle) {
        cmsArticle.setUpdateTime(DateUtils.getNowDate());
        cmsArticleMapper.deleteCmsAttachmentByArticleId(cmsArticle.getId());
        insertCmsAttachment(cmsArticle);
        return cmsArticleMapper.updateCmsArticle(cmsArticle);
    }

    /**
     * 新增文章
     *
     * @param cmsArticle 文章
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCmsArticle(CmsArticle cmsArticle) {
        if (null == cmsArticle.getTop()) {
            cmsArticle.setTop(1);
        }
        cmsArticle.setCreateBy(CmsSecurityUtils.getUsername());
        cmsArticle.setCreateTime(DateUtils.getNowDate());
        int rows = cmsArticleMapper.insertCmsArticle(cmsArticle);
        insertCmsAttachment(cmsArticle);
        return rows;
    }

    /**
     * 批量删除文章
     *
     * @param ids 需要删除的文章ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCmsArticleByIds(Long[] ids) {
        cmsArticleMapper.deleteCmsAttachmentByArticleIds(ids);
        return cmsArticleMapper.deleteCmsArticleByIds(ids);
    }

    /**
     * 删除文章信息
     *
     * @param id 文章ID
     * @return 结果
     */
    @Override
    public int deleteCmsArticleById(Long id) {
        cmsArticleMapper.deleteCmsAttachmentByArticleId(id);
        return cmsArticleMapper.deleteCmsArticleById(id);
    }

    @Override
    public int changeCategory(CmsArticleMoveCategoryVo categoryVo) {
        return cmsArticleMapper.changeCategory(categoryVo.getIds(),
                categoryVo.getCategoryId(),
                CmsSecurityUtils.getUsername(),
                DateUtils.getNowDate());
    }

    @Override
    public int changeTop(CmsArticleTopChangeVo articleTopChange) {
        return cmsArticleMapper.changeTop(articleTopChange.getIds(),
                articleTopChange.getTop(),
                CmsSecurityUtils.getUsername(),
                DateUtils.getNowDate());
    }


    /**
     * 新增附件信息
     *
     * @param cmsArticle 文章对象
     */
    public void insertCmsAttachment(CmsArticle cmsArticle) {
        List<CmsAttachment> cmsAttachmentList = cmsArticle.getCmsAttachmentList();
        Long id = cmsArticle.getId();
        if (StringUtils.isNotNull(cmsAttachmentList)) {
            List<CmsAttachment> list = new ArrayList<CmsAttachment>();
            for (CmsAttachment cmsAttachment : cmsAttachmentList) {
                cmsAttachment.setArticleId(id);
                cmsAttachment.setCreateBy(CmsSecurityUtils.getUsername());
//                cmsAttachment.setCreateTime(DateUtils.getNowDate());
                list.add(cmsAttachment);
            }
            if (list.size() > 0) {
                cmsArticleMapper.batchCmsAttachment(list);
            }
        }
    }
}
